#include <napi/native_api.h>
#include <hilog/log.h>
#include <cstdlib>
#include <cstring>
#include <string>
#include <hilog/log.h>
#include <multimedia/image_framework/image/image_common.h>
#include <multimedia/image_framework/image/image_source_native.h>
#include <multimedia/image_framework/image/pixelmap_native.h>
#include <arkui/native_interface.h>
#include <arkui/native_node.h>
#include <arkui/native_type.h>
#undef LOG_DOMAIN
#undef LOG_TAG
#define LOG_DOMAIN 0x3200
#define LOG_TAG "HeifNAPI"

// 声明 Kotlin 侧暴露的函数
extern "C" {
    int64_t create_heif_image(const char* path, float width, float height);
    void destroy_heif_image(int64_t handle);
}

// NAPI 包装：创建 HEIF 图片
static napi_value CreateHeifImage(napi_env env, napi_callback_info info) {
    size_t argc = 3;
    napi_value args[3];
    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    if (status != napi_ok || argc < 1) {
        napi_throw_error(env, nullptr, "Wrong number of arguments");
        return nullptr;
    }
    
    // 获取路径参数
    size_t pathLen;
    napi_get_value_string_utf8(env, args[0], nullptr, 0, &pathLen);
    char* path = new char[pathLen + 1];
    napi_get_value_string_utf8(env, args[0], path, pathLen + 1, &pathLen);
    
    // 获取宽高参数
    double width = 300.0, height = 300.0;  // 默认值
    if (argc >= 2) {
        napi_get_value_double(env, args[1], &width);
    }
    if (argc >= 3) {
        napi_get_value_double(env, args[2], &height);
    }
    
    OH_LOG_INFO(LOG_APP, "Creating HEIF image: %{public}s, %{public}fx%{public}f", 
                path, width, height);
    
    // 调用 Kotlin 侧的函数
    int64_t handle = create_heif_image(path, (float)width, (float)height);
    delete[] path;
    
    if (handle == 0) {
        napi_throw_error(env, nullptr, "Failed to create HEIF image");
        return nullptr;
    }
    
    // 返回句柄
    napi_value result;
    napi_create_int64(env, handle, &result);
    return result;
}

// 销毁 HEIF 图片
static napi_value DestroyHeifImage(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1];
    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    if (status != napi_ok || argc != 1) {
        napi_throw_error(env, nullptr, "Wrong number of arguments");
        return nullptr;
    }
    
    // 获取句柄参数
    int64_t handle;
    napi_get_value_int64(env, args[0], &handle);
    
    OH_LOG_INFO(LOG_APP, "Destroying HEIF image: %{public}lld", handle);
    
    // 调用 Kotlin 侧的函数
    destroy_heif_image(handle);
    
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    return undefined;
}

static napi_value GetPixelMapFromHandle(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1];
    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    if (status != napi_ok || argc != 1) {
        napi_throw_error(env, nullptr, "Wrong number of arguments");
        return nullptr;
    }

    // 获取句柄参数
    int64_t handle;
    status = napi_get_value_int64(env, args[0], &handle);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Invalid handle");
        return nullptr;
    }

    if (handle == 0) {
        napi_throw_error(env, nullptr, "Invalid handle (null)");
        return nullptr;
    }

    OH_LOG_INFO(LOG_APP, "Converting handle to PixelMap: %{public}lld", handle);

    // 转换为 PixelMap 指针
    OH_PixelmapNative* pixelmap = reinterpret_cast<OH_PixelmapNative*>(handle);

    // 创建 ArkTS 可用的 PixelMap 对象
    napi_value jsPixelMap = nullptr;

    // 使用 OH_PixelmapNative_ConvertPixelmapNativeToNapi 将 Native PixelMap 转换为 NAPI 对象
    int32_t result = OH_PixelmapNative_ConvertPixelmapNativeToNapi(env, pixelmap, &jsPixelMap);

    if (result != 0 || jsPixelMap == nullptr) {
        OH_LOG_ERROR(LOG_APP, "Failed to convert PixelMap to NAPI, error: %{public}d", result);
        napi_throw_error(env, nullptr, "Failed to create PixelMap object");
        return nullptr;
    }

    OH_LOG_INFO(LOG_APP, "Successfully converted handle to PixelMap");
    return jsPixelMap;
}

// 模块导出函数
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        { "createHeifImage", nullptr, CreateHeifImage, nullptr, nullptr, nullptr, napi_default, nullptr },
         { "getPixelMapFromHandle", nullptr, GetPixelMapFromHandle, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "destroyHeifImage", nullptr, DestroyHeifImage, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    
    OH_LOG_INFO(LOG_APP, "HEIF NAPI module initialized");
    return exports;
}

// 模块定义
static napi_module heifModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

// 模块注册
extern "C" __attribute__((constructor)) void RegisterHeifModule(void) {
    napi_module_register(&heifModule);
}
